import { defineStore } from 'pinia';

const getPremissionList = () => {
  return new Promise<string[]>((resolve) => {
    setTimeout(() => {
      resolve(['admin']);
    }, 500);
  });
};

export const useDemoStore = defineStore('demo', {
  persist: {
    key: `m:${location.host}:DEMO_MODEL`,
    storage: window.sessionStorage, // default
  },
  state: () => {
    return {
      userInfo: {
        name: '',
        age: 0,
        sex: '',
      },
      token: '$l',
      premissionList: [] as string[],
    };
  },
  getters: {
    cName: (state) => state.userInfo.name + 'vip',
  },
  actions: {
    async updateUserList() {
      try {
        this.premissionList = await getPremissionList();
      } catch {
        // do nothing
      }
    },
    updateUserInfo(userInfo: { name: string; age: number; sex: string }) {
      this.userInfo = userInfo;
    },
    updateAge(age: number) {
      this.userInfo.age = age;
    },
    updateSex(sex: string) {
      this.userInfo.sex = sex;
    },
    updateToken(token: string) {
      this.token = token;
    },
  },
});
